/**
 * projectName: waste_recovery-version_management
 * fileName: UserLoginController.java
 * packageName: com.damie.controller
 * date: 2021-01-19 4:22 下午
 * copyright(c) 2017-2020 xxx公司
 */
package com.damie.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.damie.common.commonutils.R;
import com.damie.common.jwtuilts.JwtUtil;
import com.damie.domain.User;
import com.damie.param.UserWx;
import com.damie.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @version: V1.0
 * @author: light
 * @className: UserLoginController
 * @packageName: com.damie.controller
 * @description:
 * @data: 2021-01-19 4:22 下午
 **/
@RestController
@RequestMapping
public class UserLoginController {
    @Resource
    private UserService service;
    /**
     * @param code
     * @return token
     * @throws
     **/
    @ApiOperation(value = "用户qq登陆")
    @GetMapping("/qqlogin")
    public R qqLogin(String code) throws Exception {
        String token = service.qqLogin(code);
        return R.ok().data("token",token);
    }

    @ApiOperation(value = "微信登陆获取token")
    @GetMapping("/wxlogin")
    public R wxLogin(UserWx code) throws Exception {
        Map<String,Object> openid2 = new HashMap();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_password",code.getNickName());
        User one = service.getOne(userQueryWrapper);
        if (ObjectUtils.isEmpty(one)){
            User one1 = new User();
            one1.setUserAccount(UUID.randomUUID().toString());
            one1.setUserPassword(code.getNickName());
            one1.setUserStatus(1);
            one1.setUserRole(1);
            one1.setUserPicture(code.getAvatarUrl());
            one1.setUserIncome(new BigDecimal(0));
            if (code.getGender()!=1){
                one1.setSalt("女");
            }
            one1.setSalt("男");
            service.save(one1);
            openid2.put("userId",one1.getUserId());
            String token = JwtUtil.createToken(openid2,3600);
            return R.ok().data("token",token).data("code",one1.getUserIncome());
        }
        openid2.put("userId",one.getUserId());
        String token = JwtUtil.createToken(openid2,3600);
        return R.ok().data("token",token).data("code",one.getUserIncome());
    }
}
